package cn.csc.module.shop.service.banner;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerCreateReqVO;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerPageReqVO;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerUpdateReqVO;
import cn.csc.module.shop.controller.app.banner.vo.ShopBannerReqVO;
import cn.csc.module.shop.convert.banner.ShopBannerConvert;
import cn.csc.module.shop.dal.dataobject.banner.ShopBannerDO;
import cn.csc.module.shop.dal.mysql.banner.ShopBannerMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shop.enums.ErrorCodeConstants.SHOP_SLIDE_IMG_NOT_EXISTS;

@Service
@Validated
public class ShopBannerServiceImpl implements  ShopBannerService{

    @Resource
    private ShopBannerMapper shopBannerMapper;

    @Override
    public BigInteger createBanner(ShopBannerCreateReqVO createReqVO) {
        // 插入
        ShopBannerDO shopBannerDO = ShopBannerConvert.INSTANCE.convert(createReqVO);
        shopBannerMapper.insert(shopBannerDO);
        // 返回
        //重新排序
        updateOrderNumber(shopBannerDO);
        return shopBannerDO.getId();
    }

    @Override
    public void updateBanner(ShopBannerUpdateReqVO updateReqVO) {
        // 校验存在
        validateSlideImgExists(updateReqVO.getId());
        // 更新
        ShopBannerDO updateObj = ShopBannerConvert.INSTANCE.convert(updateReqVO);
        shopBannerMapper.updateById(updateObj);
        //重新排序
        updateOrderNumber(updateObj);
    }
    private void validateSlideImgExists(BigInteger id) {
        if (shopBannerMapper.selectById(id) == null) {
            throw exception(SHOP_SLIDE_IMG_NOT_EXISTS);
        }
    }
    @Override
    public void deleteBanner(BigInteger id) {
        // 校验存在
        validateSlideImgExists(id);
        // 删除
        shopBannerMapper.deleteById(id);
    }

    @Override
    public ShopBannerDO getBanner(BigInteger id) {
        return shopBannerMapper.selectById(id);
    }

    @Override
    public PageResult<ShopBannerDO> getBannerPage(ShopBannerPageReqVO pageReqVO) {
        return shopBannerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopBannerDO> selectBannerListNotId(ShopBannerDO bannerDO) {
        QueryWrapper queryWrapper= new QueryWrapper();
        queryWrapper.ne("id",bannerDO.getId());
        queryWrapper.orderByAsc("order_number");
        return shopBannerMapper.selectList(queryWrapper);
    }

    @Override
    public List<ShopBannerDO> selectBannerList(ShopBannerReqVO reqVO) {
        QueryWrapper queryWrapper= new QueryWrapper();
        queryWrapper.ne("status",5);
        queryWrapper.orderByAsc("order_number");
        return shopBannerMapper.selectList(queryWrapper);
    }

    private void updateOrderNumber(ShopBannerDO shopBannerDO){
        List<ShopBannerDO> list = this.selectBannerListNotId(shopBannerDO);
        int i = 1;
        for (ShopBannerDO banner : list) {
            if (i != shopBannerDO.getOrderNumber()) {
                banner.setOrderNumber(i);
                this.updateBanner(ShopBannerConvert.INSTANCE.convert01(banner));
            } else {
                i = i + 1;
                banner.setOrderNumber(i);
                this.updateBanner(ShopBannerConvert.INSTANCE.convert01(banner));
            }
            i++;
        }
    }
}
